// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2016-2018, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "lombo_clk_csp.h"

const char *clk_to_en[] = {
	"osc24m", "osc32k", "null_clk", "ax_clamp_gate", "mem_axi_clk",
};

#if defined(CONFIG_ARCH_LOMBO_N9V1)
const char *pll_linear_name[] = {"sdram_pll", "cpu_pll", "ax_pll",};
const char *pll_spread_name[] = {
	"sdram_pll", "perh0_pll_vco", "perh1_pll", "perh2_pll",
	"disp0_pll", "disp1_pll",
};

const char *ignore_unused_clk[] = {
	"ahb_sdramc_gate", "ahb_sdramc1_gate", "ahb_sdramc_reset",
	"ahb_sdramc1_reset", "ahb_sdram_pub_gate", "abh_sdram1_pub_gate",
	"sdram_rst", "sdram_clk", "amu_clk", "mem_axi1_clk", "sdram_pll",
};

#elif defined(CONFIG_ARCH_LOMBO_N9V3)
const char *pll_linear_name[] = {"sdram_pll", "disp0_pll",};
const char *pll_spread_name[] = {
	"sdram_pll", "perh0_pll_vco", "perh2_pll", "disp0_pll", "disp1_pll",
};

const char *ignore_unused_clk[] = {
	"ahb_sdramc_gate", "ahb_sdramc_reset", "sdram_rst", "sdram_clk",
	"mem_axi1_clk", "sdram_pll",
};


#elif defined(CONFIG_ARCH_LOMBO_N7V3)
const char *pll_linear_name[] = {"sdram_pll", "cpu_pll", "ax_pll",};
const char *pll_spread_name[] = {
	"sdram_pll", "perh0_pll_vco", "perh2_pll", "disp_pll",
};

const char *ignore_unused_clk[] = {
	"ahb_sdram_gate", "ahb_sdram_reset", "sdram_rst", "sdram_clk",
	"sdram_pll",
};

#elif defined(CONFIG_ARCH_LOMBO_N7V5) || defined(CONFIG_ARCH_LOMBO_N5V1)
const char *pll_linear_name[] = {"sdram_pll",};
const char *pll_spread_name[] = {"sdram_pll", "perh0_pll_vco", "perh2_pll",};

const char *ignore_unused_clk[] = {
	"ahb_sdram_gate", "ahb_sdram_reset", "sdram_rst", "sdram_clk",
	"sdram_pll",
};

#elif defined(CONFIG_ARCH_LOMBO_N5V0)
const char *pll_linear_name[] = {"sdram_pll", "sdram1_pll", "cpu_pll",};
const char *pll_spread_name[] = {
	"sdram_pll", "sdram1_pll", "perh0_pll_vco", "perh2_pll", "disp0_pll",
	"disp1_pll",
};

const char *ignore_unused_clk[] = { };
#endif

const int pll_linear_counts = ARRAY_SIZE(pll_linear_name);
const int pll_spread_counts = ARRAY_SIZE(pll_spread_name);
const int clk_to_en_counts = ARRAY_SIZE(clk_to_en);

bool is_ignore_unused_clk(const char *name)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(ignore_unused_clk); i++) {
		if (!strcmp(name, ignore_unused_clk[i]))
			return true;
	}

	return false;
}

void csp_enable_spread_during_linear(int enable, void __iomem *addr)
{
	reg_prcm_pll_tune2_t reg;

	reg.val = readl(addr);
	reg.bits.en = enable;
	writel(reg.val, addr);
}

void csp_set_linear_adjust_freq_step(u32 step, void __iomem *addr)
{
	reg_prcm_pll_tune2_t reg;

	reg.val = readl(addr);
	reg.bits.step = step;
	writel(reg.val, addr);
}

void csp_set_spread_para(u32 amplitude, u32 period, void __iomem *addr)
{
	reg_prcm_pll_tune1_t reg;

	reg.val = readl(addr);
	reg.bits.amplitude = amplitude;
	reg.bits.period = period;
	writel(reg.val, addr);
}

#if defined(CONFIG_ARCH_LOMBO_N7V3) || defined(CONFIG_ARCH_LOMBO_N9V1)
/* get the max factor value, according to pll's recommended freq-div table */
int __get_fac_max(const char *name, int pre_div)
{
	int div_max;

	if (!strcmp(name, "perh2_pll")) {
		if (pre_div == 1)
			div_max = 84;
		else if (pre_div == 2)
			div_max = 200;
		else if (pre_div == 3)
			div_max = 252;
		else if (pre_div == 4)
			div_max = 255;
		else {
			WARN_ON(1);
			div_max = 84;
		}
	} else
		div_max = (pre_div == 1 ? 84 :
				(pre_div == 2 ? 200 : (WARN_ON(1), 84)));
	return div_max;
}
#else
/* get the max factor value, according to pll's recommended freq-div table */
int __get_fac_max(const char *name, int pre_div)
{
	if (!strcmp(name, "sdram_pll") || !strcmp(name, "perh0_pll_vco"))
		return pre_div == 1 ? 73 :
			(pre_div == 2 ? 146 : (WARN_ON(1), 73));
	else if (!strcmp(name, "perh2_pll"))
		return pre_div == 1 ? 66 :
			(pre_div == 2 ? 132 : (WARN_ON(1), 66));
	else
		return pre_div == 1 ? 50 :
			(pre_div == 2 ? 100 : (WARN_ON(1), 50));
}
#endif
